package com.zjx.activiti01;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class ActivitiBusinessDemo {
    private final static String KEY = "process_evetion";

    /**
     * 添加业务key到Activiti的表
     */
    @Test
    public void addBusinessKey() {
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2. 获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        // 3.添加businessKey  第一个参数流程定义的key，第二个参数businessKey（这里指出差申请的ID）
        ProcessInstance instance = runtimeService.startProcessInstanceByKey(KEY, "1001");

        //输出
        System.out.println("businessKey==" + instance.getBusinessKey());
    }

    /**
     * 全部流程实例的挂起和激活
     * suspend 暂停
     */
    @Test
    public void suspendAllProcessInstance() {
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //3. 查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(KEY)
                .singleResult();

        //4.获取当前流程定义的实例是否是挂起状态
        boolean suspended = processDefinition.isSuspended();

        //5.获取流程定义的ID
        String processDefinitionId = processDefinition.getId();

        //6.如果是挂起状态则激活
        if (suspended) {
            //如果挂起则进行激活操作,第一个参数是流程定义的ID，参数二传是否激活，参数3传激活时间
            repositoryService.activateProcessDefinitionById(processDefinitionId, true, null);
            System.out.println("流程定义ID："+processDefinitionId+" 已经激活");
        } else {
            //7.如果是激活状态则挂起,第一个参数是流程定义的ID，参数二传是否暂停，参数3传暂停时间
            repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
            System.out.println("流程定义ID："+processDefinitionId+" 已经挂起");

        }


    }

    /**
     * 挂起或者激活单个实例（例如挂起某个人的出差申请）
     */
    @Test
    public void suspendSingleProcessInstance(){
        //1.获取流程实例
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //3.通过RuntimeService获取流程实例对象
        ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("22501")
                .singleResult();

        //4.得到流程实例的暂停状态
        boolean suspended = instance.isSuspended();

        //5.获取流程实例ID
        String instanceId = instance.getId();

        //6.判断流程实例是否暂停，如果已经暂停则执行激活操作
        if(suspended){
            //如果已经暂停则激活
            runtimeService.activateProcessInstanceById(instanceId);
            System.out.println("流程实例ID："+instanceId+"已经激活");
        }else {
            //7.如果已经激活则暂停
            runtimeService.suspendProcessInstanceById(instanceId);
            System.out.println("流程实例ID："+instanceId+"已经暂停");
        }


    }

    /**
     * 完成个人任务
     */
    @Test
    public void completTask(){
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.获取TaskService
        TaskService taskService = processEngine.getTaskService();

        //3.使用taskservice获取任务。参数1为流程实例ID，参数2为负责人
        Task task = taskService.createTaskQuery()
                .processInstanceId("22501")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例ID=="+task.getProcessInstanceId());
        System.out.println("流程任务ID=="+task.getId());
        System.out.println("负责人=="+task.getAssignee());
        System.out.println("任务名称=="+task.getName());

        //4.根据任务ID完成任务
        taskService.complete(task.getId());
    }

}
